package persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.util.ArrayList;

import negocio.Area;
import negocio.Cursada;
import negocio.Curso;
import negocio.Instructor;
import negocio.Sala;

public class CursadaDAO
{
	private static CursadaDAO instancia;
	private final Connection c;
	
	private CursadaDAO()
	{
		c = BDManager.getInstance().getConexion();
	}

	public static CursadaDAO getInstance()
	{
		if(instancia ==null)
		{
			instancia = new CursadaDAO();
		}
		return instancia;
	}
	
	public void guardar(Cursada cursada)
	{
		String query = "INSERT INTO Cursada(fecainicio, cantvacantes, estadocursada, curso_fk, instructor_fk, codigo) "
				+ "VALUES ('" + cursada.getFechaInicio().toString() +"'," + cursada.getMaxVacantes() + ",'" 
				+ cursada.getEstado() + "'," + cursada.getCurso().getId() + ".'" + cursada.getInstructor().getNombre() +"','" + cursada.getCodigo() + "')";
		
		try {
			Statement s = c.createStatement();
			s.execute(query);
			s.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public ArrayList<Cursada> getAll()
	{
		String query = "SELECT * FROM Cursada";
		Statement s;
		ResultSet rs;
		ArrayList<Cursada> res = new ArrayList<Cursada>();
		try {
			s = c.createStatement();
			rs = s.executeQuery(query);
			while(rs.next())
			{
				Curso curso= CursoDAO.getInstance().getCurso(rs.getInt(6));
				Instructor instructor = InstructorDAO.getInstance().getInstructor(Integer.parseInt(rs.getString(7)));
				res.add(new Cursada(curso, rs.getDate(2), rs.getInt(3), rs.getInt(2), instructor, rs.getString(5), rs.getString(10)));
			}
			s.close();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return res;
	}
}
